package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import domain.Supplier;
public class SupplierDao extends BaseDao{
	@SuppressWarnings("rawtypes")
	public ArrayList select(String sql)throws Exception{
		ArrayList<Supplier>result =new ArrayList<Supplier>();
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		conn=getConnection();
		pstmt=conn.prepareStatement(sql);
		rs=pstmt.executeQuery();
		while(rs.next()){
			Supplier obj=new Supplier();
			obj.setSuppid(rs.getString("suppid"));
			obj.setName(rs.getString("name"));
			obj.setStatus(rs.getString("status"));
			obj.setAddr1(rs.getString("addr1"));
			obj.setAddr2(rs.getString("addr2"));
			obj.setCity(rs.getString("city"));
			obj.setState(rs.getString("state"));
			obj.setZip(rs.getString("zip"));
			obj.setPhone(rs.getString("phone"));			
			result.add(obj);
		}
		rs.close();
		pstmt.close();
		conn.close();
		return result;	
	}
	public void insert(Supplier obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="insert into supplier values(?,?,?,?,?,?,?,?,?)";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getSuppid());
		ps.setString(2, obj.getName());
		ps.setString(3, obj.getStatus());
		ps.setString(4, obj.getAddr1());
		ps.setString(5, obj.getAddr2());
		ps.setString(6, obj.getCity());
		ps.setString(7, obj.getState());
		ps.setString(8, obj.getZip());
		ps.setString(9, obj.getPhone());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	public void update(Supplier obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="update supplier set name=?,status=?,addr1=?,addr2=?,city=?,state=?,zip=?,phone=? where suppid=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getName());	
		ps.setString(2, obj.getStatus());
		ps.setString(3, obj.getAddr1());
		ps.setString(4, obj.getAddr2());	
		ps.setString(5, obj.getCity());
		ps.setString(6, obj.getState());
		ps.setString(7, obj.getZip());
		ps.setString(8, obj.getPhone());
		ps.setString(9, obj.getSuppid());	
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	public void delete(Supplier obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="delete from supplier where suppid=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getSuppid());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	@SuppressWarnings("rawtypes")
	public Supplier getSupplier(String SuppId) throws Exception{
		Supplier obj=null;
		ArrayList list=select("select*from supplier where catid='"+SuppId+"'");
		if(list.size()>0){
			obj=(Supplier) list.get(0);
		}
		return obj;
	}

}
